<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\api\controller;


use app\api\service\MemberService;
use common\controller\BaseController;
use common\Token\Token;

/**
 * Class Member
 * @package app\api\controller\v1
 */
class Member extends BaseController
{
    protected $no_token = true;

    public function register()
    {
        $MemberService = MemberService::instance();
        $input_extract_rule = ['phone' => '', 'password' => '', 'member_name' => ''];
        $post = input_extract_rule($input_extract_rule, 'post');
        $token = $MemberService->register($post);
        return $this->json_responce(['token' => $token]);
    }

    public function login()
    {
        $MemberService = MemberService::instance();
        $input_extract_rule = ['phone' => '', 'password' => '', 'captcha' => ''];
        $post = input_extract_rule($input_extract_rule, 'post');
        $token = $MemberService->loginVerify($post);
        return $this->json_responce(['token' => $token]);
    }

    public function captcha()
    {
        $config = [
            'length' => 4,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    /**
     * 微信小程序登陆
     * @param $code
     * @return string
     * @throws \ReflectionException
     * @throws \common\exception\BaseException
     */
    public function wxCodeLogin($code)
    {
        return $this->json_responce([
            'token' => Token::instance()->wx_program_login($code),
            'param_map' => config('param_map_config.')
        ]);;
    }

    public function verify()
    {
        Token::instance()->verifyToken(input('token'));
        $member = Token::instance()->getUserInfo();
        $member->visible(['member_name', 'member_id']);
        return $this->json_responce([
            'user_info' => $member,
            'param_map' => config('param_map_config.')
        ]);
    }
}
